package com.ophelisis.common.util.xstream;

import org.apache.log4j.helpers.OptionConverter;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

public class PropsConverter implements Converter {

	@SuppressWarnings("rawtypes")
	public boolean canConvert(Class clazz) {
		return clazz.equals(String.class);
	}

	public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {
		writer.setValue((String) value);
	}

	public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
		return OptionConverter.substVars(reader.getValue(), System.getProperties());
	}

}
